package com.mao.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.mao.model.store.Store;
import com.mao.model.user.User;
import com.mao.model.user.UserVO;
import com.mao.service.store.IStoreService;
import com.mao.service.store.IUserService;
import com.mao.util.MD5Util;
import com.mao.util.SessionHelper;

public class MainFrameAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private IUserService<User> userService;
	private IStoreService<Store> storeService;
	private User user;
	private List<User> userList;
	private Store store;

	private Long storeId;
	private Long userId;
	private String loginName;
	private String password;
	private String rePassword;

	private Integer page = 0;
	private Integer rows = 0;
	private String code;
	private Map<String, Object> result;

	

	public String main() {
		return SUCCESS;
	}

	// 某个店铺的所有用户
	public String usersOfStore() {
		result = new HashMap<String, Object>();
		User sessionUser = SessionHelper.getSessionUser();
		if (sessionUser != null) {
			store = userService.getStoreofUser(sessionUser.getStore().getId());
			storeId = store.getId();
			UserVO uservo = new UserVO();
			uservo.setStoreId(storeId);
			uservo.setLoginName(loginName);
			userList = userService.getUsersOfStore(uservo, page, rows);
			int userCount = userService
					.getUsersCountOfStore(uservo, page, rows);

			List<UserVO> userVOList = new ArrayList<UserVO>();
			UserVO userVO = null;
			User user = null;
			for (int i = 0; i < userList.size(); i++) {
				userVO = new UserVO();
				user = userList.get(i);
				userVO.setId(user.getId());
				userVO.setName(user.getName());
				userVO.setLoginName(user.getLoginName());
				userVO.setPassword(user.getPassword());
				userVO.setRePassword(user.getPassword());
				userVO.setStoreId(storeId);
				userVO.setStoreName(store.getCompanyName());
				userVO.setIsAdmin(user.getIsAdmin());
				userVO.setIsOperated(user.getIsOperated());
				userVO.setPhone(user.getPhone());
				userVO.setAddress(user.getAddress());
				userVO.setNote(user.getNote());
				userVOList.add(userVO);
			}

			result.put("total", userCount);
			result.put("rows", userVOList);
		}

		return "userManage";
	}

	public String saveUser() {
		User sessionUser = SessionHelper.getSessionUser();
		storeId = sessionUser.getStore().getId();
		userId = sessionUser.getId();
		password = user.getPassword();
		if(password.equals(rePassword)){
			String pwd = MD5Util.MD5(password);// 加密
			user.setPassword(pwd);
		}
		user.setIsAdmin(0);
		user.setIsOperated(0);
		user.setCreateTime(new Date());
		user.setParent(sessionUser);
		user.setStore(storeService.load(storeId));
		userService.saveUser(user);

		return "saveUser";
	}

	public String updateUser() {
		User user2 = userService.getUserById(user.getId());
		//如果页面的密码和数据库里面的密码不同，则说明页面密码修改了，那么在此加密，反正则不变
		if (!user.getPassword().equals(user2.getPassword())) {
			String pwd = MD5Util.MD5(user.getPassword());
			user.setPassword(pwd);
		}
		user.setIsAdmin(user2.getIsAdmin());
		user.setIsOperated(user2.getIsOperated());
		user.setParent(user2.getParent());
		user.setCreateTime(user2.getCreateTime());
		user.setLoginName(user2.getLoginName());
		user.setLoginTime(user2.getLoginTime());
		user.setStatus(user2.getStatus());
		user.setStore(user2.getStore());
		userService.updateUser(user);

		return "updateUser";
	}

	public String toUserManage() {
		return "toUserManage";
	}

	public String deleteUser() {
		User user2 = userService.getUserById(user.getId());
		user2.setStatus(User.STATUS_DELETED);
		userService.updateUser(user2);
		return "deleteUser";
	}

	public String toUpdatePassword() {
		return "toUpdatePassword";
	}

	public String updatePassword() {
		User user = userService.getUserById(SessionHelper.getSessionUser().getId());
		String pwd = MD5Util.MD5(password);
		user.setPassword(pwd);
		userService.updateUser(user);
		return "toUpdatePassword";
	}

	public String quit() {
		SessionHelper.setUser(null);
		return "index";
	}

	
	
	
	public IUserService<User> getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(IUserService<User> userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Store getStore() {
		return store;
	}

	public void setStore(Store store) {
		this.store = store;
	}

	public Long getStoreId() {
		return storeId;
	}

	public void setStoreId(Long storeId) {
		this.storeId = storeId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public IStoreService<Store> getStoreService() {
		return storeService;
	}

	public void setStoreService(IStoreService<Store> storeService) {
		this.storeService = storeService;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}
	
	

}
